package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportPurchaseInventory;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportPurchaseInventoryDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-12-24 15:51:59
 */
public class TransportPurchaseInventoryConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportPurchaseInventoryConverts.class);

    public static List<TransportPurchaseInventoryDTO> convert(List<TransportPurchaseInventory> transportPurchaseInventoryList) {
        if (CollectionUtils.isEmpty(transportPurchaseInventoryList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportPurchaseInventoryDTO> transportPurchaseInventoryListResult = Lists.newArrayList();
        for (TransportPurchaseInventory transportPurchaseInventory : transportPurchaseInventoryList) {
            transportPurchaseInventoryListResult.add(convert(transportPurchaseInventory));
        }
        return transportPurchaseInventoryListResult;
    }

    public static TransportPurchaseInventoryDTO convert(TransportPurchaseInventory transportPurchaseInventory) {
        try {
            return BeanUtils.copy(transportPurchaseInventory, TransportPurchaseInventoryDTO.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseInventory到TransportPurchaseInventoryDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseInventory到TransportPurchaseInventoryDTO转换失败", e);
        }
    }

    public static TransportPurchaseInventory convert(TransportPurchaseInventoryDTO transportPurchaseInventoryDTO) {
        try {
            return BeanUtils.copy(transportPurchaseInventoryDTO, TransportPurchaseInventory.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseInventoryDTO到TransportPurchaseInventory转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseInventoryDTO到TransportPurchaseInventory转换失败", e);
        }
    }
}